#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>

#include <iot_debug.h>
#include <iot_fs.h>
#include <iot_socket.h>

#include "shell/log.h"

void ls_show_file_info(AMOPENAT_FS_FIND_DATA* rs)
{
  logInfo("%s\t%-16d\t%s",
    ((rs->st_mode&E_FS_ATTR_ARCHIVE)? "FILE":"DIR"), rs->st_size, rs->st_name);
}

int cmd_ls(int argc, char** argv)
{
  if(argc < 2) {
    logInfo("usage error.");
    return -1;
  }

  char* dir_name = argv[1];
  AMOPENAT_FS_FIND_DATA findResult;
  INT32 iFd = -1;

  iFd = iot_fs_find_first(dir_name, &findResult);

  ls_show_file_info(&findResult);

  while(iot_fs_find_next(iFd, &findResult) == 0) {
    ls_show_file_info(&findResult);
  }

  if(iFd >= 0) {
    iot_fs_find_close(iFd);
  }

  return 0;
}

// 系统重启
int reboot(int argc, char** argv)
{
  logInfo("rebooting...");
  iot_os_restart();
  return 0;
}
